<?php

$dir = dirname(__FILE__);

require_once("FileHandler.php");

class Message{
	private $id;
	private $text;
	private $parent;
	private $time;
	private $user;
	private $parentRepliesNumber;
	
	private $fh;
	private $db;

	public function __construct($id=null){
		$this->id = $id;

		$this->db = new Database;
		$this->cmm = new Communicator;
	}

	public function getId(){
		return $this->id;
	}

	public function getParent(){
		return $this->parent;
	}
	
	public function setMessage($user,$text,$time=null,$parent=null){
		if($time==null) $time = date('Y-d-m h:i:s');
		if(!is_numeric($parent)){
			$parent = null;
			$this->parentRepliesNumber = null;
		} 
		else $this->resolveParentRepliesNumber($parent);

		$this->text = $text;
		$this->user = $user;
		$this->time = $time;
		$this->parent = $parent;
	}

	public function getArray(){
		return array('user'=>$this->user->getId(),'text'=>$this->text,'time'=>$this->time,'parent'=>$this->parent,'session'=>$this->user->getSession(), 'parent_replies_number'=>$this->parentRepliesNumber);
	}
	
	public function isReply(){
		if(is_numeric($this->parent)) return true;

		return false;
	}

	private function resolveParentRepliesNumber($parent = null){
		if($parent == null) $parent = $this->parent;
		$res = $this->db->execute('SELECT parent_replies_number FROM reply WHERE message = '.$parent);
	
		if($this->db->getNumberOfRows() > 0){
			$row = mysql_fetch_array($res,MYSQL_NUM);
			$this->parentRepliesNumber = $row[0]+1;
		}else $this->parentRepliesNumber = 1;
	}

	public function getTree($params){
		$room = new Room($params['room']);
		$this->id = $params['message'];

		$fh = new FileHandler($room->getPath());

		$replyContent = $fh->getFileContent('reply');
		$replyObject = json_decode($replyContent);

		$response;
		if(isset($params['root'])){
			$rootMessage = $params['root'];

			$response = array($rootMessage);
		}else{
			$rootMessage = $this->retrieveRoot($replyObject->parent);
			$response = array($this->id, $rootMessage);
		}		

		$tree = $this->retrieveTree($rootMessage,$replyObject->children);

		$response[] = $tree;

		echo json_encode($response);
	}

	public function getTrackToRoot($params){
		$id = $this->id = $params['message'];

		$query = "SELECT T2.id
					FROM (
					    SELECT
					        @r AS _id,
					        (SELECT @r := parent FROM message WHERE id = _id) AS parent,
					        @l := @l + 1 AS lvl
					    FROM
					        (SELECT @r := $id, @l := 0) vars,
					        message h
					    WHERE @r <> 0) T1
					JOIN message T2
					ON T1._id = T2.id
					ORDER BY T1.lvl DESC";

		$res = $this->db->execute($query);

		$track = array();
		while($row = mysql_fetch_array($res,MYSQL_NUM)) $track[] = $row[0];
		
		echo json_encode(array($id,$track[0],$track));
	}

	public function retrieveTree($id, $tree, $replies=array()){
		$replies[] = $id;		
		
		if(isset($tree->$id)){
			$children = $tree->$id;

			for($i=0,$iMax=count($children); $i<$iMax; $i++){
				$childId = $children[$i];
				$replies = $this->retrieveTree($childId, $tree, $replies);
			}			
		}		

		return $replies;
	}

	public function retrieveRoot($tree){
		$message = $this->id;

		while(isset($tree->$message)){ //go up until find the root
			$message = $tree->$message;
		}

		return $message;
	}

	public function getMessages($params){
		$ids = implode(',',$params['ids']);
	
		$query = "SELECT * FROM message WHERE id IN ($ids)";

		$this->db->execute($query);

		echo json_encode($this->db->toArray(MYSQL_NUM));
	}

	public function getParentRepliesNumber(){
		return $this->parentRepliesNumber;
	}

}

?>